from pathlib import Path
import PyPDF2
import os
import glob


def merge_pdfs(pdf_files, output_path):
    merger = PyPDF2.PdfMerger()
    for pdf_file in pdf_files:
        with open(pdf_file, 'rb') as file:
            merger.append(file)
    with open(output_path, 'wb') as output_file:
        merger.write(output_file)


file = os.chdir(r'C:\Users\Admin\Desktop\mooc')
list_of_files = os.listdir(file)

# 示例用法，指定要合并的PDF文件列表以及输出合并后文件的路径
pdf_files = [name for name in list_of_files if name.startswith('pydsa')]  # 替换为实际的PDF文件名
print(pdf_files)
output_path = "6-排序与查找.pdf"  # 输出合并后的文件名
merge_pdfs(pdf_files, output_path)
